import { createRouter, createWebHistory } from 'vue-router'
import UserView from '../views/UserView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
    },
    {
      path: '/register',
      name: 'register',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/RegisterView.vue'),
    },
    {
      path: '/user',
      name: 'user',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: UserView,
    },
    {
      path: '/admin',
      name: 'admin',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AdminView.vue'),
    },
  ],
})
// router.beforeEach(async (to,from,next)=>{
//   var auth =localStorage.getItem('token');
//   const path = localStorage.getItem('path');
//   if(to.path=="/" || to.path == '/register'){
//     if(auth){
//       router.push(`/${path}`)
//     }else{
//       next()
//     }
//   }else if( to.path == '/user'){
//     if(auth){
//       if(path == 'admin'){
//         router.push('/admin')
//       }else{
//         next()
//       }
//     }else{
//       router.push('/')
//     }
//   }else{
//     if(auth){
//       if(path == 'admin'){
//         next()
//       }else{
//         router.push('/user')
//       }
//     }else{
//       router.push('/')
//     }
//   }
// })
export default router
